﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using MMK.Paket_Tracker.GUI.Views;
using MMK.Paket_Tracker.Runtime;
using MMK.Paket_Tracker.Runtime.BusinessEntities;
using MMK.Paket_Tracker.Runtime.Commands;
using MMK.Paket_Tracker.Runtime.Interfaces;
using MMK.Paket_Tracker.Runtime.Workflow;

namespace MMK.Paket_Tracker.GUI
{
    
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IMainWindowNotification
    {
        #region public methods
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            MergeDictionaries();
            UpdateDateTimeLabels();
            StartTimer();
            LoadStartUC();
        }

        /// <summary>
        /// Gets or sets the notifications of UserControls to the main window.
        /// </summary>
        /// <value>The notification for the main window.</value>
        public MainWindowNotification NotifyMainWindow
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                this.UCGrid.Children.Clear();
                this.UCGrid.Children.Add(PTWorkflow.GetNextState(value));
                this.lblSubHeader.Content = ((INamedUserControl)this.UCGrid.Children[0]).ViewName;
            }
        }

                       
        #endregion

        #region private methods
        private void MergeDictionaries()
        {
            ResourceDictionary resourceDictionary = new ResourceDictionary();
            Uri uri = new Uri("pack://siteoforigin:,,,/ExternalDict.xaml", UriKind.Absolute);
            resourceDictionary.Source = uri;

            this.Resources.MergedDictionaries.Add(resourceDictionary);
        }

        private void StartTimer()
        {
            _timer = new DispatcherTimer();
            _timer.Tick += new EventHandler(OnTimerTick);
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Start();
        }

        private void OnTimerTick(object source, EventArgs e)
        {
            UpdateDateTimeLabels();
        }

        private void UpdateDateTimeLabels()
        {
            lblDate.Content = DateTime.Now.ToShortDateString();
            lblTime.Content = DateTime.Now.ToString("HH:mm:ss");
        }

        private void LoadStartUC()
        {
            StartWindow uc = new StartWindow();
            this.UCGrid.Children.Add(uc);
            this.lblSubHeader.Content = uc.ViewName;
        }

        #endregion

        #region private Variables
        private DispatcherTimer _timer;
        #endregion

    }
}
